package cn.interestingshop.dao.user;

import cn.interestingshop.dao.BaseDaoImpl;
import cn.interestingshop.dao.user.UserDao;
import cn.interestingshop.entity.User;
import cn.interestingshop.utils.EmptyUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : UserDaoImpl
 * @Description :
 * @Author : Lzz
 * @Date: 2023/2/20 14:11:39
 */
public class UserDaoImpl extends BaseDaoImpl implements UserDao {

    public UserDaoImpl(Connection connection) {
        super(connection);
    }

    /**
     * 查询用户列表
     * @param currentPageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<User> findAllUserList(Integer currentPageNo, Integer pageSize) {
        List<User> userList = new ArrayList<>();    //返回的用户集合
        ResultSet rs = null;
        try {
            String sql = "select id,account,password,nickName,gender,idCardNo,email,phone,type from t_user  ";
            sql += "limit "+(currentPageNo-1)*pageSize+","+pageSize;
            rs = this.executeQuery(sql, null);
            while (rs.next()){
                User user = createEntityByResultSet(rs);
                userList.add(user);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
                this.closeResource(rs);
                this.closeResource();
        }
        return userList;
    }

    /**
     * 查询用户总记录条数
     * @return
     */
    @Override
    public int findUserCount() {
        ResultSet rs = null;
        int count = 0;      //总记录条数
        try {
            String sql = "select count(1) from t_user ";
            rs = this.executeQuery(sql,null);
            if(rs.next()){
                count = rs.getInt(1);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            this.closeResource(rs);
            this.closeResource();
        }
        return count;
    }

    /**
     *
     * @param id
     * @param account
     * @return
     */
    @Override
    public User selectById(Integer id, String account) {
        User user = null;
        ResultSet rs = null;
        List<Object> parmas = new ArrayList<>();
        String sql = "select id,account,nickName,password,gender,idCardNo,email,phone,type from t_user where 1=1 ";
        try {
            //判断ID是否为空
            if(!EmptyUtils.isEmpty(id)){
                sql += " and id=? ";
                parmas.add(id);
            }
            if(!EmptyUtils.isEmpty(account)){
                sql += " and account=? ";
                parmas.add(account);
            }
            rs = this.executeQuery(sql, parmas.toArray());
            if(rs.next()){
                user = createEntityByResultSet(rs);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            this.closeResource(rs);
            this.closeResource();
        }
        return user;
    }

    @Override
    public int save(User user) {
        int userId = 0;
        String sql = "INSERT into t_user(account,nickName,password,gender,idCardNo,email,phone) values(?,?,?,?,?,?,?)";
        try {
            Object[] parmas = new Object[]{user.getAccount(),user.getNickName(),user.getPassword(),user.getGender(),user.getIdCardNo(),user.getEmail(),user.getPhone()};
            userId = this.executeInsert(sql, parmas);
            user.setId(userId);
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            this.closeResource();
        }
        return userId;
    }

    @Override
    public User createEntityByResultSet(ResultSet rs) throws Exception {
        User user = new User();
        user.setId(rs.getInt("id"));
        user.setAccount(rs.getString("account"));
        user.setPassword(rs.getString("password"));
        user.setNickName(rs.getString("nickName"));
        user.setGender(rs.getInt("gender"));
        user.setIdCardNo(rs.getString("idCardNo"));
        user.setEmail(rs.getString("email"));
        user.setPhone(rs.getString("phone"));
        user.setType(rs.getInt("type"));
        return user;
    }
}
